<template>
    <div class="home" style="flex: 1;padding: 10px;">
        <!-- 功能区域 -->
        <div style="padding: 10px 0">
            <el-button type="primary" @click="add">新增</el-button>
            <el-button type="primary">导入</el-button>
            <el-button type="primary">导出</el-button>
        </div>
        <!-- 搜索区域 -->
        <div style="margin: 10px 0; ">
            <el-input v-model="search" placeholder="请输入关键字" style="width: 20%;" clearable></el-input>
            <el-button type="primary" style="margin-left:  10px;" @click="load">查询</el-button>
        </div>
        <el-table :data="tableData" border stripe style="width: 100%">
            <el-table-column prop="id" label="ID" />
            <el-table-column prop="username" label="用户名" />
            <el-table-column prop="nickName" sortable label="昵称" />
            <el-table-column prop="age" sortable label="年龄" />
            <el-table-column prop="sex" sortable label="性别" />
            <el-table-column prop="address" sortable label="地址" />
            <el-table-column fixed="right" label="Operations">
                <template #default="scope">
                    <el-button type="primary" size="small" @click="handleClick(scope.row)">编辑</el-button>
                    <el-popconfirm title="确认删除吗?" @confirm="handleDelete(scope.row.id)">
                        <template #reference>
                            <el-button type="danger" size="small" >删除</el-button>
                        </template>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <div style="margin: 10px auto;">
            <el-pagination style="margin: 0 auto;" v-model:current-page="currentPage" v-model:page-size="pageSize"
                :page-sizes="[5, 10, 20]" :small="small" :disabled="disabled" :background="background"
                layout="total, sizes, prev, pager, next, jumper" v-if="total" :total="total"
                @size-change="handleSizeChange" @current-change="handleCurrentChange" />

            <el-dialog v-model="dialogVisible" title="提示" width="30%">
                <el-form :model="form" label-width="auto" style="max-width: 600px">
                    <el-form-item label="用户名">
                        <el-input v-model="form.username" />
                    </el-form-item>
                    <el-form-item label="昵称">
                        <el-input v-model="form.nickName" />
                    </el-form-item>
                    <el-form-item label="年龄">
                        <el-input v-model="form.age" />
                    </el-form-item>
                    <el-form-item label="性别">
                        <el-radio v-model="form.sex" value="男" size="large">男</el-radio>
                        <el-radio v-model="form.sex" value="女" size="large">女</el-radio>
                        <el-radio v-model="form.sex" value="未知" size="large">未知</el-radio>
                    </el-form-item>
                    <el-form-item label="地址">
                        <el-input type="textarea" v-model="form.address" />
                    </el-form-item>
                </el-form>
                <template #footer>
                    <div class="dialog-footer">
                        <el-button @click="dialogVisible = false">取消</el-button>
                        <el-button type="primary" @click="save" :plain="true">
                            确定
                        </el-button>
                    </div>
                </template>
            </el-dialog>
        </div>
    </div>
</template>
<script setup lang="ts" name="Home">
import { onBeforeMount, reactive, ref } from 'vue';
// import { ElMessageBox, ElNotification } from 'element-plus'
import { ElMessage } from 'element-plus'
import type { TableColumnCtx } from 'element-plus'
import request from '../utils/request';

const form = ref({})
const dialogVisible = ref(false)
const search = ref()
const total = ref()
const currentPage = ref(1)
const pageSize = ref(10)
const small = ref(false)
const disabled = ref(false)
const background = ref(false)
let tableData = ref([])

const handleSizeChange = (pageSize: number) => {
    pageSize = pageSize
    load()
}
const handleCurrentChange = (pageNUM: number) => {
    currentPage.value = pageNUM
    load()
}

function handleClick(row: any) {
    dialogVisible.value = true
    form.value = JSON.parse(JSON.stringify(row))
}

function handleDelete(id:any){
    // console.log(id);
    request.delete('/user/'+id).then(res =>{
        if(res.code === '0'){
                ElMessage({
                message: '删除成功',
                type: 'success' 
            })
            }
        else {
                ElMessage({
                message: res.msg,
                type: 'error'
            })
            } 
        load()
    })  
}

onBeforeMount(() => {
    load()
    // if(sessionStorage.getItem("username") == null)
    // router.push('/login')
})


function load() {
    request.get('/user', {
        params: {
            pageNum: currentPage.value,
            pageSize: pageSize.value,
            search: search.value
        }
    }).then(res => {
        tableData.value = res.data.records
        total.value = res.data.total
        currentPage.value = res.data.current
        pageSize.value = res.data.size
    })
}


function add() {
    dialogVisible.value = true
    form.value = {}
} 
function save() {
    if (form.value.id) {
        request.put('/user', form.value).then(res => {
            // console.log(res);
            if(res.code === '0'){
                ElMessage({
                message: '修改成功',
                type: 'success' 
            })
            }
            else {
                ElMessage({
                message: res.msg,
                type: 'error'
            })
            } 
            dialogVisible.value = false
            load()
        })
    }
    else {
        request.post('/user', form.value).then(res => {
            console.log(res);
            dialogVisible.value = false
            ElMessage({
                // title:'提示',
                message: '新增成功',
                type: 'success' 
            })
            load()
        })
    }

}

</script>

<style scoped></style>
